added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / jswrappedmethod.cs
blobdf8720e3cdfa009451e3bf94150d917c38e99eb7
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
17 namespace Microsoft.JScript {
19 using System;
20 using System.Reflection;
21 using System.Reflection.Emit;
22 using System.Globalization;
23 using System.Diagnostics;
25 internal sealed class JSWrappedMethod : JSMethod, IWrappedMember{
26 internal MethodInfo method;
27 private ParameterInfo[] pars;
29 internal JSWrappedMethod(MethodInfo method, Object obj)
30 : base(obj) {
31 this.obj = obj;
32 if (method is JSMethodInfo) method = ((JSMethodInfo)method).method;
33 this.method = method.GetBaseDefinition();
34 this.pars = this.method.GetParameters();
35 if (obj is JSObject && !Typeob.JSObject.IsAssignableFrom(method.DeclaringType))
36 if (obj is BooleanObject) this.obj = ((BooleanObject)obj).value;
37 else if (obj is NumberObject) this.obj = ((NumberObject)obj).value;
38 else if (obj is StringObject) this.obj = ((StringObject)obj).value;
39 else if (obj is ArrayWrapper) this.obj = ((ArrayWrapper)obj).value;
42 public override MethodAttributes Attributes{
43 get{
44 return this.method.Attributes;
48 private Object[] CheckArguments(Object[] args){
49 Object[] newArgs = args;
50 if (args != null && args.Length < this.pars.Length){
51 newArgs = new Object[this.pars.Length];
52 ArrayObject.Copy(args, newArgs, args.Length);
53 for (int i = args.Length, size = this.pars.Length; i < size; i++)
54 newArgs[i] = System.Type.Missing; // this will take care of the default value
56 return newArgs;
59 internal override Object Construct(Object[] args){
60 if (this.method is JSMethod)
61 return ((JSMethod)(this.method)).Construct(args);
62 if (this.method.GetParameters().Length == 0 && this.method.ReturnType == Typeob.Object){
63 Object func = this.method.Invoke(this.obj, BindingFlags.SuppressChangeType, null, null, null);
64 if (func is ScriptFunction)
65 return ((ScriptFunction)func).Construct(args);
67 throw new JScriptException(JSError.NoConstructor);
70 public override Type DeclaringType{
71 get{
72 return this.method.DeclaringType;
76 internal override String GetClassFullName(){
77 if (this.method is JSMethod)
78 return ((JSMethod)this.method).GetClassFullName();
79 else
80 return this.method.DeclaringType.FullName;
83 internal override PackageScope GetPackage(){
84 if (this.method is JSMethod){
85 return ((JSMethod)this.method).GetPackage();
87 return null;
90 public override ParameterInfo[] GetParameters(){
91 return this.pars;
94 internal override MethodInfo GetMethodInfo(CompilerGlobals compilerGlobals){
95 if (this.method is JSMethod)
96 return ((JSMethod)(this.method)).GetMethodInfo(compilerGlobals);
97 else
98 return this.method;
101 public Object GetWrappedObject(){
102 return this.obj;
105 #if !DEBUG
106 [DebuggerStepThroughAttribute]
107 [DebuggerHiddenAttribute]
108 #endif
109 public override Object Invoke(Object obj, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture){
110 parameters = CheckArguments(parameters);
111 return this.Invoke(this.obj, this.obj, options, binder, parameters, culture);
114 #if !DEBUG
115 [DebuggerStepThroughAttribute]
116 [DebuggerHiddenAttribute]
117 #endif
118 internal override Object Invoke(Object obj, Object thisob, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture){
119 parameters = CheckArguments(parameters);
120 if (this.obj != null && !(this.obj is Type)) obj = this.obj;
121 if (this.method is JSMethod)
122 return ((JSMethod)this.method).Invoke(obj, thisob, options, binder, parameters, culture);
123 else
124 return this.method.Invoke(obj, options, binder, parameters, culture);
127 public override String Name{
128 get{
129 return this.method.Name;
133 public override Type ReturnType{
134 get{
135 return this.method.ReturnType;
139 public override String ToString(){
140 return this.method.ToString();